package com.mmx.baseservice.system.tools.timer.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mmx.baseservice.common.mybatisplus.structure.CustomQueryWrapper;
import com.mmx.baseservice.common.utils.FileUtil;
import com.mmx.baseservice.common.utils.LogUtil;
import com.mmx.baseservice.system.tools.build.SysParamsBuild;
import com.mmx.baseservice.system.tools.timer.util.TimerExecuteUtil;
import com.mmx.baseservice.system.pojo.SysDbBackup;
import com.mmx.baseservice.system.service.SysDbBackupService;
import org.quartz.JobExecutionContext;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("deleteBackDataTimer")
@EnableScheduling
public class DeleteBackDataTimer extends QuartzJobBean {

    @Resource
    private SysDbBackupService sysDbBackupService;

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) {
//        TimerExecuteUtil.run(2, ()->{
//            LogUtil.info("--------定时器自动删除备份--------");
//
//            // 查询删除的备份信息
//            String delBackupNums = SysParamsBuild.getParamsByKey("activeBackupNum");
//            QueryWrapper<SysDbBackup> queryWrapper = new CustomQueryWrapper<>();
//            queryWrapper.orderByAsc(SysDbBackup.CREATE_TIME);
//            List<SysDbBackup> backupList = sysDbBackupService.list(queryWrapper);
//
//            if (backupList.size() > Integer.parseInt(delBackupNums)) {
//                for (int i = 0; i < backupList.size(); i++) {
//                    if (backupList.size() - i >= Integer.parseInt(delBackupNums)) {
//                        sysDbBackupService.removeById(backupList.get(i).getId());
//                        // 删除源文件
//                        FileUtil.delete(backupList.get(i).getPath());
//                    } else {
//                        break;
//                    }
//                }
//            }
//            LogUtil.info("--------定时器自动删除备份结束，删除数量：{}--------", backupList.size() - Integer.parseInt(delBackupNums));
//        });
    }

}
